package dmmm.shemes;


import java.awt.Point;
import java.util.ArrayList;

import dmmm.components.*;
import dmmm.gui.GuiLabel;
import dmmm.gui.GuiLine;
import dmmm.gui.GuiScheme;

public class Fetch1 {

    private static REG PC;
    private static TSB TSBPCout2, TSBPCHout2, TSBIRDAout1, TSBIRJAout1, TSBIRPOMout1, TSBIRBRout1;
    private static IntToInt PChigh, IBUS2lowINT;
    private static REG IR0, IR1, IR2, IR3;
    private static IntToBools IR0bits, IR1bits, IR2bits, IR3bits;
    private static BoolsToInt IRDAint, IRJAint, IRPOMint, IRBRint, IR23_8xToINT;
    //public static BoolsToInt JDISP, INTENTRY, PCRDISP, IMM;
    // potrebni samo za iscrtavanje linija za prosirivanje znakom ili nulom
    private static GuiScheme gui;

    public static GuiScheme getGui() {
        return gui;
    }

    public static void init() {

        PC = new REG(1, "PC");
        PC.getOut(0).setIsInt();
        PC.getOut(0).setNumOfLines(16);
        PC.initVal(256);//100h

        TSBPCout2 = new TSB();
        TSBPCout2.getOut(0).setNumOfLines(16);

        PChigh = new IntToInt(16, 8, true, false);

        TSBPCHout2 = new TSB();
        TSBPCHout2.getOut(0).setNumOfLines(8);

        IBUS2lowINT = new IntToInt(16, 8);

        IR0 = new REG(1, "IR0");
        IR0.getOut(0).setIsInt();
        IR0.getOut(0).setNumOfLines(8);
        IR1 = new REG(1, "IR1");
        IR1.getOut(0).setIsInt();
        IR1.getOut(0).setNumOfLines(8);
        IR2 = new REG(1, "IR2");
        IR2.getOut(0).setIsInt();
        IR2.getOut(0).setNumOfLines(8);
        IR3 = new REG(1, "IR3");
        IR3.getOut(0).setIsInt();
        IR3.getOut(0).setNumOfLines(8);
        
        IR0bits = new IntToBools(8, 8);
        IR1bits = new IntToBools(8, 8);
        IR2bits = new IntToBools(8, 8);
        IR3bits = new IntToBools(8, 8);
        IR23_8xToINT = new BoolsToInt(8, 8);

        IRDAint = new BoolsToInt(16, 16);
        IRJAint = new BoolsToInt(16, 16);
        IRPOMint = new BoolsToInt(16, 16);
        IRBRint = new BoolsToInt(8, 8);

        TSBIRDAout1 = new TSB();
        TSBIRDAout1.getOut(0).setNumOfLines(16);

        TSBIRJAout1 = new TSB();
        TSBIRJAout1.getOut(0).setNumOfLines(16);

        TSBIRPOMout1 = new TSB();
        TSBIRPOMout1.getOut(0).setNumOfLines(16);

        TSBIRBRout1 = new TSB();
        TSBIRBRout1.getOut(0).setNumOfLines(8);


    }

    public static void connect() {
        PC.setInputPin(0, Bus1.IBUS1());
        PC.setPinInc(Oper1.incPC());
        PC.setPinLd(Oper1.ldPC());

        TSBPCout2.setInputPin(0, PC.getOut(0));
        TSBPCout2.setCtrl(Oper1.PCout2());
        Bus1.addOnIBUS2(TSBPCout2.getOut(0));

        PChigh.setInputPin(0, PC.getOut(0));

        TSBPCHout2.setInputPin(0, PChigh.getOut(0));
        TSBPCHout2.setCtrl(Oper1.PCHout2());
        Bus1.addOnIBUS2(TSBPCHout2.getOut(0));

        IBUS2lowINT.setInputPin(0, Bus1.IBUS2());

        IR0.setPinLd(Oper1.ldIR0());
        IR1.setPinLd(Oper1.ldIR1());
        IR2.setPinLd(Oper1.ldIR2());
        IR3.setPinLd(Oper1.ldIR3());
        IR0.setInputPin(0, IBUS2lowINT.getOut(0));
        IR1.setInputPin(0, IBUS2lowINT.getOut(0));
        IR2.setInputPin(0, IBUS2lowINT.getOut(0));
        IR3.setInputPin(0, IBUS2lowINT.getOut(0));
        
        IR0bits.setInputPin(0, IR0.getOut(0));
        IR1bits.setInputPin(0, IR1.getOut(0));
        IR2bits.setInputPin(0, IR2.getOut(0));
        IR3bits.setInputPin(0, IR3.getOut(0));
        
        for (int i = 0; i < 8; i++) {
            IRDAint.setInputPin(i, IR3bits.getOut(i));
            IRDAint.setInputPin(8 + i, IR2bits.getOut(i));
        }

        for (int i = 0; i < 8; i++) {
            IRJAint.setInputPin(i, IR2bits.getOut(i));
            IRJAint.setInputPin(8 + i, IR1bits.getOut(i));
        }

        for (int i = 0; i < 8; i++) {
            IRPOMint.setInputPin(i, IR1bits.getOut(i));
            IRPOMint.setInputPin(8 + i, IR1bits.getOut(7));
            IR23_8xToINT.setInputPin(i, IR1bits.getOut(7));
        }

        for (int i = 0; i < 8; i++) {
            IRBRint.setInputPin(i, IR0bits.getOut(i));
        }

        TSBIRDAout1.setInputPin(0, IRDAint.getOut(0));
        TSBIRDAout1.setCtrl(Oper1.IRDAout1());
        Bus1.addOnIBUS1(TSBIRDAout1.getOut(0));

        TSBIRJAout1.setInputPin(0, IRJAint.getOut(0));
        TSBIRJAout1.setCtrl(Oper1.IRJAout1());
        Bus1.addOnIBUS1(TSBIRJAout1.getOut(0));

        TSBIRPOMout1.setInputPin(0, IRPOMint.getOut(0));
        TSBIRPOMout1.setCtrl(Oper1.IRPOMout1());
        Bus1.addOnIBUS1(TSBIRPOMout1.getOut(0));

        TSBIRBRout1.setInputPin(0, IRBRint.getOut(0));
        TSBIRBRout1.setCtrl(Oper1.IRBRout1());
        Bus1.addOnIBUS1(TSBIRBRout1.getOut(0));

    }

    public static void gui() {
        GuiLine line; // Pomocna promenljiva
        gui = new GuiScheme("dmmm/dmmm/images/Fetch1.png");

        ArrayList<ArrayList<Point>> sections;
        ArrayList<Point> points;


        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(92, 15));
        points.add(new Point(92, 711));
        sections.add(points);
        points = new ArrayList<Point>();
        points.add(new Point(94, 15));
        points.add(new Point(94, 711));
        sections.add(points);
        line = new GuiLine(sections, Bus1.IBUS1());
        gui.addLine(line);


        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(583, 15));
        points.add(new Point(583, 711));
        sections.add(points);
        points = new ArrayList<Point>();
        points.add(new Point(585, 15));
        points.add(new Point(585, 711));
        sections.add(points);
        line = new GuiLine(sections, Bus1.IBUS2());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(95, 53));
        points.add(new Point(399, 53));
        points.add(new Point(399, 71));
        sections.add(points);
        line = new GuiLine(sections, Bus1.IBUS1());
        gui.addLine(line);


        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(316, 81));
        points.add(new Point(323, 81));
        sections.add(points);
        line = new GuiLine(sections, Oper1.incPC());
        gui.addLine(line);


        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(316, 100));
        points.add(new Point(323, 100));
        sections.add(points);
        line = new GuiLine(sections, Oper1.ldPC());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(476, 91));
        points.add(new Point(482, 91));
        sections.add(points);
        line = new GuiLine(sections, LogicalComponent.getCPUCLK());
        gui.addLine(line);


        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(399, 111));
        points.add(new Point(399, 183));
        points.add(new Point(485, 183));
        sections.add(points);
        points = new ArrayList<Point>();
        points.add(new Point(399, 136));
        points.add(new Point(485, 136));
        sections.add(points);
        line = new GuiLine(sections, PC.getOut(0));
        gui.addLine(line);


        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(497, 142));
        points.add(new Point(497, 148));
        sections.add(points);
        line = new GuiLine(sections, Oper1.PCout2());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(497, 189));
        points.add(new Point(497, 194));
        sections.add(points);
        line = new GuiLine(sections, Oper1.PCHout2());
        gui.addLine(line);


        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(509, 136));
        points.add(new Point(582, 136));
        sections.add(points);
        line = new GuiLine(sections, TSBPCout2.getOut(0));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(509, 183));
        points.add(new Point(582, 183));
        sections.add(points);
        line = new GuiLine(sections, TSBPCHout2.getOut(0));
        gui.addLine(line);


        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(583, 230));
        points.add(new Point(255, 230));
        points.add(new Point(255, 252));
        sections.add(points);
        points = new ArrayList<Point>();
        points.add(new Point(471, 231));
        points.add(new Point(471, 252));
        sections.add(points);
        line = new GuiLine(sections, IBUS2lowINT.getOut(0));
        gui.addLine(line);


        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(172, 263));
        points.add(new Point(179, 263));
        sections.add(points);
        line = new GuiLine(sections, LogicalComponent.getCPUCLK());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(172, 282));
        points.add(new Point(179, 282));
        sections.add(points);
        line = new GuiLine(sections, Oper1.ldIR0());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(255, 292));
        points.add(new Point(255, 310));
        sections.add(points);
        line = new GuiLine(sections, IR0.getOut(0));
        gui.addLine(line);


        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(388, 263));
        points.add(new Point(394, 263));
        sections.add(points);
        line = new GuiLine(sections, LogicalComponent.getCPUCLK());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(388, 282));
        points.add(new Point(394, 282));
        sections.add(points);
        line = new GuiLine(sections, Oper1.ldIR1());
        gui.addLine(line);


        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(471, 292));
        points.add(new Point(471, 310));
        sections.add(points);
        line = new GuiLine(sections, IR1.getOut(0));
        gui.addLine(line);


        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(582, 336));
        points.add(new Point(255, 336));
        points.add(new Point(255, 358));
        sections.add(points);
        points = new ArrayList<Point>();
        points.add(new Point(471, 336));
        points.add(new Point(471, 358));
        sections.add(points);
        line = new GuiLine(sections, IBUS2lowINT.getOut(0));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(172, 369));
        points.add(new Point(179, 369));
        sections.add(points);
        line = new GuiLine(sections, LogicalComponent.getCPUCLK());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(172, 387));
        points.add(new Point(179, 387));
        sections.add(points);
        line = new GuiLine(sections, Oper1.ldIR2());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(255, 398));
        points.add(new Point(255, 416));
        sections.add(points);
        line = new GuiLine(sections, IR2.getOut(0));
        gui.addLine(line);


        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(384, 369));
        points.add(new Point(394, 369));
        sections.add(points);

        line = new GuiLine(sections, LogicalComponent.getCPUCLK());
        gui.addLine(line);
        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(384, 387));
        points.add(new Point(394, 387));
        sections.add(points);
        line = new GuiLine(sections, Oper1.ldIR3());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(471, 398));
        points.add(new Point(471, 416));
        sections.add(points);
        line = new GuiLine(sections, IR3.getOut(0));
        gui.addLine(line);


        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(263, 469));
        points.add(new Point(289, 469));
        sections.add(points);
        line = new GuiLine(sections, IR2.getOut(0));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(263, 484));
        points.add(new Point(289, 484));
        sections.add(points);
        line = new GuiLine(sections, IR3.getOut(0));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(190, 476));
        points.add(new Point(224, 476));
        sections.add(points);
        line = new GuiLine(sections, IRDAint.getOut(0));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(178, 482));
        points.add(new Point(178, 488));
        sections.add(points);
        line = new GuiLine(sections, Oper1.IRDAout1());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(95, 476));
        points.add(new Point(168, 476));
        sections.add(points);
        line = new GuiLine(sections, TSBIRDAout1.getOut(0));
        gui.addLine(line);


        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(289, 525));
        points.add(new Point(263, 525));
        sections.add(points);
        line = new GuiLine(sections, IR1.getOut(0));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(289, 540));
        points.add(new Point(263, 540));
        sections.add(points);
        line = new GuiLine(sections, IR2.getOut(0));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(224, 533));
        points.add(new Point(190, 533));
        sections.add(points);
        line = new GuiLine(sections, IRJAint.getOut(0));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(178, 538));
        points.add(new Point(178, 544));
        sections.add(points);
        line = new GuiLine(sections, Oper1.IRJAout1());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(168, 533));
        points.add(new Point(95, 533));
        sections.add(points);
        line = new GuiLine(sections, TSBIRJAout1.getOut(0));
        gui.addLine(line);


        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(263, 582));
        points.add(new Point(289, 582));
        sections.add(points);
        line = new GuiLine(sections, IR23_8xToINT.getOut(0));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(263, 597));
        points.add(new Point(289, 597));
        sections.add(points);
        line = new GuiLine(sections, IR1.getOut(0));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(190, 590));
        points.add(new Point(224, 590));
        sections.add(points);
        line = new GuiLine(sections, IRPOMint.getOut(0));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(178, 595));
        points.add(new Point(178, 601));
        sections.add(points);
        line = new GuiLine(sections, Oper1.IRPOMout1());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(95, 590));
        points.add(new Point(167, 590));
        sections.add(points);
        line = new GuiLine(sections, TSBIRPOMout1.getOut(0));
        gui.addLine(line);



        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(225, 646));
        points.add(new Point(190, 646));
        sections.add(points);
        line = new GuiLine(sections, IR0.getOut(0));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(178, 652));
        points.add(new Point(178, 658));
        sections.add(points);
        line = new GuiLine(sections, Oper1.IRBRout1());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(167, 646));
        points.add(new Point(95, 646));
        sections.add(points);
        line = new GuiLine(sections, TSBIRBRout1.getOut(0));
        gui.addLine(line);

        // LABELE:
        gui.addLabel(new GuiLabel(87, 10, Bus1.IBUS1()));
        gui.addLabel(new GuiLabel(98, 454, Bus1.IBUS1()));
        gui.addLabel(new GuiLabel(578, 10, Bus1.IBUS2()));
        gui.addLabel(new GuiLabel(589, 454, Bus1.IBUS2()));
        gui.addLabel(new GuiLabel(404, 55, Bus1.IBUS1()));
        gui.addLabel(new GuiLabel(414, 149, PC.getOut(0)));
        gui.addLabel(new GuiLabel(414, 197, PChigh.getOut(0)));
        gui.addLabel(new GuiLabel(534, 149, TSBPCout2.getOut(0)));
        gui.addLabel(new GuiLabel(534, 197, TSBPCHout2.getOut(0)));
        gui.addLabel(new GuiLabel(267, 244, IBUS2lowINT.getOut(0)));
        gui.addLabel(new GuiLabel(483, 244, IBUS2lowINT.getOut(0)));
        gui.addLabel(new GuiLabel(267, 349, IBUS2lowINT.getOut(0)));
        gui.addLabel(new GuiLabel(483, 349, IBUS2lowINT.getOut(0)));
        gui.addLabel(new GuiLabel(249, 323, IR0.getOut(0)));
        gui.addLabel(new GuiLabel(465, 323, IR1.getOut(0)));
        gui.addLabel(new GuiLabel(249, 429, IR2.getOut(0)));
        gui.addLabel(new GuiLabel(465, 429, IR3.getOut(0)));
        gui.addLabel(new GuiLabel(195, 470, IRDAint.getOut(0)));
        gui.addLabel(new GuiLabel(195, 527, IRJAint.getOut(0)));
        gui.addLabel(new GuiLabel(195, 584, IRPOMint.getOut(0)));
        gui.addLabel(new GuiLabel(195, 640, IRBRint.getOut(0)));
        gui.addLabel(new GuiLabel(117, 471, TSBIRDAout1.getOut(0)));
        gui.addLabel(new GuiLabel(117, 529, TSBIRJAout1.getOut(0)));
        gui.addLabel(new GuiLabel(117, 586, TSBIRPOMout1.getOut(0)));
        gui.addLabel(new GuiLabel(117, 659, TSBIRBRout1.getOut(0)));

    }

    public static Pin IR31() {
        return IR0bits.getOut(7);
    }

    public static Pin IR30() {
        return IR0bits.getOut(6);
    }

    public static Pin IR29() {
        return IR0bits.getOut(5);
    }

    public static Pin IR28() {
        return IR0bits.getOut(4);
    }

    public static Pin IR27() {
        return IR0bits.getOut(3);
    }

    public static Pin IR26() {
        return IR0bits.getOut(2);
    }

    public static Pin IR25() {
        return IR0bits.getOut(1);
    }

    public static Pin IR24() {
        return IR0bits.getOut(0);
    }

    public static Pin IR23() {
        return IR1bits.getOut(7);
    }

    public static Pin IR22() {
        return IR1bits.getOut(6);
    }

    public static Pin IR21() {
        return IR1bits.getOut(5);
    }

    public static Pin PC() {
        return PC.getOut(0);
    }

    public static REG RegPC() {
        return PC;
    }

    public static REG RegIR0() {
        return IR0;
    }

    public static REG RegIR1() {
        return IR1;
    }

    public static REG RegIR2() {
        return IR2;
    }

    public static REG RegIR3() {
        return IR3;
    }
}
